package com.panshi.controll;

import com.panshi.AnswerApi;
import com.panshi.api.util.PageSizeVO;
import com.panshi.bean.*;
import com.panshi.service.AnswerService;
import com.panshi.util.UploadFileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.mail.MessagingException;
import java.util.List;

@Api(value="回答问题Controller层",tags={"操作回答问题接口"})
@RequestMapping("ans")
@RestController
@Slf4j
@CrossOrigin
public class AnswerControll implements AnswerApi {
    @Resource
    private AnswerService service;

    @PostMapping("/initAnswer")
    @ApiOperation("用户回答某个问题")
    public int initAnswer(@RequestBody AnswerNoBean ansNo) {
        log.info("ansNo==={}",ansNo);
        return service.initAnswer(ansNo);
    }

    @PostMapping("/uploadImg")
    @ApiOperation("图片上传")
    public String uploadImg(@RequestParam("file") MultipartFile file){
        System.out.println(UploadFileUtil.uploadImg(file)+"=============================");
        return UploadFileUtil.uploadImg(file);
    }


    @GetMapping("/deleteAnswer")
    @ApiOperation("用户删除某问题的回答(当state为0的:通过->删除问题表，为1删除未审核表)")
    public int deleteAnswer(@RequestBody AnswerBean2 answer) {
        if(answer.getAaid()!=0){//删除未审核表
            return service.updateQaAnswerNo(answer.getAaid());
        }else{//删除回答表
            service.updateQa(answer.getAid());//问题表删问题
            service.updateQaAnswerCom(answer.getAaid());//删除该回答下的所有评论
            service.updateQaAnswerLike(answer.getAaid());//删除该回答下的所有点赞

            service.deleteAnswerCount(answer.getUid());//该用户的回答总数-1
            service.deleteAnswerDynamic(answer.getAaid());//删除动态表数据

            return service.updateQaAnswer(answer.getAaid());//当删除回答表问题
        }
    }

    @GetMapping("/openCom")
    @ApiOperation("用户点击某个回答打开回复")
    public List<AnswerComBean> openCom(@RequestParam int aid, @RequestParam int pid){
        return service.openCom(aid,pid);
    }

    @PostMapping("/updateAnswer")
    @ApiOperation("用户修改回答")
    public int updateAnswer(@RequestBody AnswerBean2 answer){
        if(answer.getAaid()!=0){//调未审核表
            return service.updateAnswerNo(answer);
        }else{//调回答表
            System.out.println("修改问题表==========");
            return service.updateAnswer(answer);
        }
    }


    @GetMapping("/clientLike")
    @ApiOperation("用户给回答点赞or点踩")
    public String clientLike(@RequestParam int aaid, @RequestParam int uid, @RequestParam int type){
        AnswerLikeBean alb=service.findLike(aaid,uid);//查询用户该回答是否点赞或踩过
        if(alb==null){
            System.out.println("为空说明用户未点赞===================");
            if(type==0){ //type:0点赞  else点踩
                service.addLike(aaid);//去答案表的点赞数+1
                System.out.println("点赞===================");
                service.clientLike(aaid,uid);
            }else{
                service.addNo(aaid);//去答案表的点踩数+1
                System.out.println("点踩===================");
                service.clientNo(aaid,uid);
            }
            return "0";//成功
        }else{
            return "1";//已经点过赞了
        }
    }






    //管理员接口==========================================================================================================
    @PostMapping("/adminFindAnswer")
    @ApiOperation("管理员查询回答表")
    public PageSizeVO<List<AnswerBean2>> adminFindAnswer(@RequestBody PageSizeVO pageSizeVO){
        log.info("pageSizeVO================{}",pageSizeVO);
        pageSizeVO.setStartIndex(pageSizeVO.getStartIndexInfo());//获取起始页

        //封装返回前台
        PageSizeVO<List<AnswerBean2>> ps=new PageSizeVO<>();

        log.info("state:{}=========",pageSizeVO.getState());
        if(pageSizeVO.getState()=="" || pageSizeVO.getState()==null){//查询全部
            pageSizeVO.setState(null);
            ps.setData(service.adminFindAnswer(pageSizeVO));
            ps.setCount(service.adminFindAnswerSum(Integer.valueOf(10)));//总数
        }else{//根据状态查询
            ps.setData(service.adminFindAnswer(pageSizeVO));
            ps.setCount(service.adminFindAnswerSum(Integer.valueOf(pageSizeVO.getState())));//总数
        }
        log.info("ps================{}",ps);
        return ps;
    }

    @PostMapping("/adminFindAnswerNo")
    @ApiOperation("管理员查询回答审核表")
    public PageSizeVO<List<AnswerNoBean>> adminFindAnswerNo(@RequestBody PageSizeVO pageSizeVO){
        log.info("pageSizeVO================{}",pageSizeVO);
        pageSizeVO.setStartIndex(pageSizeVO.getStartIndexInfo());//获取起始页


        //封装返回前台
        PageSizeVO<List<AnswerNoBean>> ps=new PageSizeVO<>();
        log.info("state:{}=========",pageSizeVO.getState());
        if(pageSizeVO.getState()=="" || pageSizeVO.getState()==null){//查询全部
            pageSizeVO.setState(null);
            ps.setData(service.adminFindAnswerNo(pageSizeVO));
            ps.setCount(service.adminFindAnswerNoSum(10));//总数
        }else{//根据状态查询
            ps.setData(service.adminFindAnswerNo(pageSizeVO));
            ps.setCount(service.adminFindAnswerNoSum(Integer.valueOf(pageSizeVO.getState())));//总数
        }
        log.info("ps================{}",ps);
        return ps;
    }

    @PostMapping("/adminInitAnswer")
    @ApiOperation("审核表回答进入回答表")
    public int adminInitAnswer(@RequestBody AnswerNoBean answer) throws MessagingException {
        AnswerNoBean anb=service.findbyAidNo(answer.getAaid());//根据答案id去审核表查数据
        System.out.println(anb+"anb*-----------------");

        service.setemailanswerno(anb.getUid(),3,anb.getQ_title());//审核完后发送邮件通知用户
        service.updateQaAnswerNo(anb.getAaid());//审核通过，删除回答审核表的数据

        IssBean iss=service.FindbyQid(anb.getAid());//查询回答属于哪个问题
        System.out.println(iss+"///////////////////");
        answer.setQ_title(iss.getQ_title());
        service.addQaInstation(answer);//存入站内消息表

        service.updateUserData(anb.getUid());//用户表的回答数+1

        int answer_id=service.adminInitAnswer(anb);//返回主键
        System.out.println(answer_id+"-------------");


        return service.addDynamic(answer_id,answer.getUid());//将回答存入动态表;
    }

    @GetMapping("/adminUpdateQaAnswer")
    @ApiOperation("管理员修改回答表")
    public int adminUpdateQaAnswer(@RequestParam int state,@RequestParam int aaid,@RequestParam int uid) throws MessagingException {
        AnswerBean2 ab=service.findbyAid(aaid);//根据答案id查询答案信息
        System.out.println(ab+"-------------");
        if(state==1){//上架
            if(ab.getState()!=1){//下架改上架了(不是上架改上架)
                service.updateUserData(ab.getUid());//用户表的回答数+1
            }

            service.setemailanswer(uid,1,ab.getQ_title());//审核完后发送邮件通知用户
            int d=service.findDynamic(aaid);//判断动态表是否存在这个回答
            if(d==0){
                System.out.println("动态表不存在此数据");
                service.addDynamic(aaid,uid);//上架时新增动态表数据
            }
        }else if(state==2){//下架
            if(ab.getState()!=2){//上架改下架了(不是下架改下架)
                service.deleteAnswerCount(ab.getUid());//用户表的回答数+1
            }


            service.setemailanswer(uid,2,ab.getQ_title());//审核完后发送邮件通知用户
            service.deleteAnswerDynamic(aaid);//下架时删除动态表数据
        }
        return service.adminUpdateQaAnswer(state,aaid);
    }

    @PostMapping("/adminAnswerNo")
    @ApiOperation("管理员修改审核表")
    public int adminAnswerNo(@RequestBody AnswerNoBean answer) throws MessagingException {
        AnswerNoBean anb=service.findbyAidNo(answer.getAaid());//根据答案id去审核表查数据
        System.out.println(anb+"-------------");
        service.setemailanswerno(anb.getUid(),2,anb.getQ_title());//审核完后发送邮件通知用户
        return service.adminAnswerNo(answer);
    }
}